package com.heer.gymnasium.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.view.Display;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;

import java.util.concurrent.ExecutionException;

/**
 * Created by rzw2 on 2016/9/19.
 */
public class MyImageGetter implements Html.ImageGetter {
    private Context context;
    private TextView tv;

    public MyImageGetter(Context context, TextView tv) {
        this.context = context;
        this.tv = tv;
    }

    @Override
    public Drawable getDrawable(String source) {
        if (source.contains("/upload/image")) {
            URLDrawable drawable = new URLDrawable(
                    context.getResources().getDrawable(R.mipmap.ic_launcher));
            new AsyncThread(drawable).execute(LoadImageUtil.getLoadImageUrl(source));
            return drawable;
        }
        return null;
    }

    private class AsyncThread extends AsyncTask<String, Void, Drawable> {
        private URLDrawable drawable;

        public AsyncThread(URLDrawable drawable) {
            this.drawable = drawable;
        }

        @Override
        protected Drawable doInBackground(String... strings) {
            Bitmap bitmap = null;
            try {
                bitmap = Glide.with(context).load(strings[0]).asBitmap().into(300, 300).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return new BitmapDrawable(bitmap);
        }

        public void onProgressUpdate(Integer... value) {

        }

        @Override
        protected void onPostExecute(Drawable result) {
            if (result != null) {
                drawable.setDrawable(result);
                tv.setText(tv.getText()); // 通过这里的重新设置 TextView 的文字来更新UI
            }
        }
    }

    public class URLDrawable extends BitmapDrawable {

        private Drawable drawable;

        public URLDrawable(Drawable defaultDraw) {
            setDrawable(defaultDraw);
        }

        private void setDrawable(Drawable nDrawable) {
            drawable = nDrawable;
            drawable.setBounds(getDefaultImageBounds(drawable));
            setBounds(getDefaultImageBounds(drawable));
        }

        @Override
        public void draw(Canvas canvas) {
            // TODO Auto-generated method stub
            drawable.draw(canvas);
        }
    }

    @SuppressWarnings("deprecation")
    public Rect getDefaultImageBounds(Drawable nDrawable) {
        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = (int) (width * nDrawable.getIntrinsicHeight() / nDrawable.getIntrinsicWidth());
        Rect bounds = new Rect(0, 0, width, height);
        return bounds;
    }
}
